package com.eat.service.impl;

import com.eat.context.BaseContext;
import com.eat.dto.SetmealDTO;
import com.eat.dto.ShoppingCartDTO;
import com.eat.entity.Dish;
import com.eat.entity.ShoppingCart;
import com.eat.mapper.DishMapper;
import com.eat.mapper.SetmealMapper;
import com.eat.mapper.ShoppingCartMapper;
import com.eat.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class ShopingCartServiceImp implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void add(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        if (shoppingCarts!=null&&shoppingCarts.size()>0){
            ShoppingCart cart = shoppingCarts.get(0);
           cart.setNumber(cart.getNumber()+1);

           shoppingCartMapper.updateNumberById(cart);

        } else {
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if(dishId!=null){
                //本次添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);

                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());

            }else {
                //否则是套餐
                SetmealDTO setmealDTO = setmealMapper.getById(setmealId);

                shoppingCart.setNumber(1);
                shoppingCart.setName(setmealDTO.getName());
                shoppingCart.setAmount(setmealDTO.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());

                shoppingCart.setImage(setmealDTO.getImage());

            }
            shoppingCartMapper.insert(shoppingCart);
        }

    }
    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> getByUserId(Long currentId) {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        return list;
    }
    /**
     * 清空购物车
     * @return
     */
    @Override
    public void cleanShoppingCart() {

        Long currentId = BaseContext.getCurrentId();

        shoppingCartMapper.deleteByUserId(currentId);

    }


}
